<?php
namespace modules\user;
use \modules\ui;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;
use \modules\contact\api as contact;
class page
{
    static function mapping()
    {
        return [
            '/add' => 'add',
            '/active/#page' => 'active',
            '/archived/#page' => 'archived',
            '/profile' => 'my_profile',
            '/password' => 'edit_my_password',
            '/profile/#id' => 'profile',
            '/password/#id' => 'edit_password',
            '/role/#id' => 'edit_role',
            '/edit/#id' => 'edit_others',
        ];
    }

    static function active($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $user = auth::validate($q, $args);
        $list = api::list_active($q, $args);

        $args['SUBTITLE'] = "在职用户";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('添加用户', 'user-plus'), '/user/add'],
        ]);

        foreach ($list as &$item) {
            $id       = $item['id'];
            $name     = $item['name'];
            $role     = ['admin' => '管理员', 'salesman' => '业务员'][$item['role']];
            $username = $item['username'];
            $item = [
                "$role $name", "$username", "href" => "/user/profile/$id",
                [
                    [ui::icon_label('修改密码', 'key'    ), $id === $user['id'] ? null : "/user/password/$id"],
                    [ui::icon_label('修改角色', 'user-md'), $id === $user['id'] ? null : "/user/role/$id"],
                    [ui::icon_label('编辑详情', 'edit'   ), "/user/edit/$id"],
                    [ui::icon_label('离职'    , 'ban'    ), "data-api" => "/api/user/leave", "data-api-id" => $id],
                ],
            ];
        }

        $pager = ui::pager(function ($page) {
            return "/user/active/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "在职用户");
    }

    static function archived($q, $html, &$args)
    {
        $page = arr::get_or_bad_request($args, 'page');
        $list = api::list_archived($q, $args);

        $args['SUBTITLE'] = "已离职用户";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        foreach ($list as &$item) {
            $id       = $item['id'];
            $name     = $item['name'];
            $role     = ['admin' => '管理员', 'salesman' => '业务员'][$item['role']];
            $username = $item['username'];
            $item = [
                "$role $name", "$username", "href" => "/user/profile/$id",
                [
                    [ui::icon_label('修改密码', 'key'    ), "/user/password/$id"],
                    [ui::icon_label('修改角色', 'user-md'), "/user/role/$id"],
                    [ui::icon_label('编辑详情', 'edit'   ), "/user/edit/$id"],
                    [ui::icon_label('复职'    , 'check-circle-o'), "data-api" => "/api/user/revive", "data-api-id" => $id],
                ],
            ];
        }

        $pager = ui::pager(function ($page) {
            return "/user/archived/$page";
        }, $page, count($list) === 10);

        return ui::card(['.', ui::list($list), $pager], "已离职用户");
    }

    static function my_profile($q, $html, &$args)
    {
        $profile  = api::my_profile($q, $args);
        $name     = $profile['name'];
        $username = $profile['username'];
        $role     = $profile['role'];
        $orgname  = $profile['orgname'];
        $comment  = $profile['comment'];

        $args['SUBTITLE'] = "我的资料";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('修改密码', 'key'), '/user/password'],
        ]);

        return ui::card(ui::readonly_form($html, [
            ['text', '姓名'    , $name    ],
            ['text', '用户名'  , $username],
            ['enum', '角色'    , [$role, 'admin' => '管理员', 'salesman' => '业务员'], 'name' => 'role'],
            ['text', '隶属组织', $orgname ],
            ['longtext', '备注', $comment ],
        ]), "我的资料");
    }

    static function edit_my_password($q, $html, &$args)
    {
        $user     = auth::validate($q, $args);
        $username = $user['username'];

        $args['SUBTITLE'] = "修改我的密码";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, '/api/user/edit-my-password',
            ui::icon_label('修改我的密码', 'edit'), [
                ['text', '用户名', $username, 'readonly' => true],
                ['password', '旧密码', 'name' => 'old-password', 'maxlength' => 64],
                ['password', '新密码', 'name' => 'new-password', 'maxlength' => 64],
                ['password', '确认新密码', 'data-same-value' => 'new-password', 'maxlength' => 64],
            ], [
                [ui::icon_label('返回我的资料', 'angle-left'), '/user/profile'],
            ], '/auth/login'),
        "修改我的密码");
    }

    static function profile($q, $html, &$args)
    {
        $profile  = api::profile($q, $args);
        $id       = $profile['id'];
        $name     = $profile['name'];
        $username = $profile['username'];
        $role     = $profile['role'];
        $orgname  = $profile['orgname'];
        $comment  = $profile['comment'];

        $user = auth::validate_with_role($q, $args, 'admin');
        $is_self = ($id === $user['id']);

        $args['SUBTITLE'] = "用户 $name 的资料";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('修改密码', 'key'    ), $is_self ? null : "/user/password/$id"],
            [ui::icon_label('修改角色', 'user-md'), $is_self ? null : "/user/role/$id"],
            [ui::icon_label('编辑详情', 'edit'   ), "/user/edit/$id"],
        ]);

        return ui::card(ui::readonly_form($html, [
            ['text', '用户名'  , $username],
            ['text', '姓名'    , $name    ],
            ['enum', '角色'    , [$role, 'admin' => '管理员', 'salesman' => '业务员'], 'name' => 'role'],
            ['text', '隶属组织', $orgname ],
            ['longtext', '备注', $comment ],
        ]), "用户 $name 的资料");
    }

    static function add($q, $html, &$args)
    {
        $user = auth::validate_with_role($q, $args, 'admin');
        $orgname = $user['orgname'];

        $args['SUBTITLE'] = "添加用户";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, '/api/user/add',
            ui::icon_label('添加用户', 'user-plus'), [
                ['text'    , '隶属组织', $orgname            , 'readonly' => true],
                ['text'    , '姓名'    , 'name' => 'name'    , 'maxlength' => 64],
                ['enum'    , '角色'    , 'name' => 'role'    , ['salesman', 'admin' => '管理员'  , 'salesman' => '业务员']],
                ['text'    , '用户名'  , 'name' => 'username', 'maxlength' => 64],
                ['password', '密码'    , 'name' => 'password', 'maxlength' => 64],
                ['longtext', '备注'    , 'name' => 'comment' , null, true],
            ], [
                [ui::icon_label('取消', 'angle-left'), '/user/active/1'],
            ], '/user/active/1'),
        "添加用户");
    }

    static function edit_password($q, $html, &$args)
    {
        $profile  = api::profile($q, $args);
        $id       = $profile['id'];
        $username = $profile['username'];
        $name     = $profile['name'];

        $args['SUBTITLE'] = "修改密码";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, "/api/user/edit-password",
            ui::icon_label('修改密码', 'edit'), [
                ['text'    , '用户名', $username, 'readonly' => true],
                ['password', '新密码', 'name' => 'new-password', 'maxlength' => 64],
                ['hidden'  , ""      , 'name' => 'id', $id],
            ], [
                [ui::icon_label('取消', 'angle-left'), "/user/profile/$id"],
            ], "/user/profile/$id"),
        "修改 $name 的密码");
    }

    static function edit_role($q, $html, &$args)
    {
        $profile  = api::profile($q, $args);
        $id       = $profile['id'];
        $name     = $profile['name'];
        $username = $profile['username'];
        $role     = $profile['role'];

        $args['SUBTITLE'] = "修改角色";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, "/api/user/edit-role",
            ui::icon_label('修改角色', 'edit'), [
                ['text'  , '用户名', $username, 'readonly' => true],
                ['enum'  , '角色'  , 'name' => 'role', [$role, 'admin' => '管理员', 'salesman' => '业务员']],
                ['hidden', ""      , 'name' => 'id', $id],
            ], [
                [ui::icon_label('取消', 'angle-left'), "/user/profile/$id"],
            ], "/user/profile/$id"),
        "修改 $name 的角色");
    }

    static function edit_others($q, $html, &$args)
    {
        $profile  = api::profile($q, $args);
        $id       = $profile['id'];
        $name     = $profile['name'];
        $orgname  = $profile['orgname'];
        $username = $profile['username'];
        $role     = $profile['role'];
        $comment  = $profile['comment'];
        $args['SUBTITLE'] = "修改详情";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return ui::card(ui::form($html, '/api/user/edit-others',
            ui::icon_label('修改详情', 'user-plus'), [
                ['text'    , '隶属组织', $orgname , 'readonly' => true],
                ['text'    , '用户名'  , $username, 'readonly' => true],
                ['text'    , '姓名'    , $name    , 'name' => 'name', 'maxlength' => 64],
                ['longtext', '备注'    , $comment , 'name' => 'comment', true],
                ['hidden'  , ""        , $id      , 'name' => 'id'],
            ], [
                [ui::icon_label('取消', 'angle-left'), "/user/profile/$id"],
            ], "/user/profile/$id"),
        "修改 $name 的详细资料");
    }
}

